/*
 * ========================================================
 * Copyright(c) 2014 杭州偶尔科技-版权所有
 * ========================================================
 * 本软件由杭州偶尔科技所有, 未经书面许可, 任何单位和个人不得以
 * 任何形式复制代码的部分或全部, 并以任何形式传播。
 * 公司网址
 * 
 * 			http://www.kkkd.com/
 * 
 * ========================================================
 */
package com.simon.mengkou.ui.activity;

import android.os.Bundle;
import android.view.MenuItem;
import android.widget.EditText;

import com.ouertech.android.agm.lib.base.future.core.AgnettyFuture;
import com.ouertech.android.agm.lib.base.future.core.AgnettyResult;
import com.ouertech.android.agm.lib.base.utils.UtilString;
import com.ouertech.android.agm.lib.ui.base.MenuTxt;
import com.ouertech.android.agm.lib.ui.base.defaults.activity.BaseTopActivity;
import com.simon.mengkou.R;
import com.simon.mengkou.data.bean.base.Address;
import com.simon.mengkou.future.base.OuerFutureListener;
import com.simon.mengkou.system.constant.CstOuer;
import com.simon.mengkou.system.global.OuerApplication;
import com.simon.mengkou.utils.UtilOuer;
import com.simon.mengkou.utils.UtilValidate;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * @author : Zhenshui.Xia
 * @since : 2016/3/20.
 * desc    : 新建收货地址
 */
public class EditAddressActivity extends BaseTopActivity {
    //姓名
    @Bind(R.id.add_address_id_name)
    EditText mEtName;

    //手机号码
    @Bind(R.id.add_address_id_phone)
    EditText mEtPhone;

    //省市区
    @Bind(R.id.add_address_id_address)
    EditText mEtAddress;

    //详细地址
    @Bind(R.id.add_address_id_detail)
    EditText mEtDetail;
    //
    private Address mAddress;

    @Override
    protected void init(Bundle savedInstanceState) {
        super.init(savedInstanceState);
        mAddress = (Address) getIntent().getSerializableExtra(CstOuer.KEY.ADDRESS);
        if(mAddress == null) {
            mAddress = new Address();
        }
    }

    @Override
    protected void initTop() {
        if(UtilString.isBlank(mAddress.getId())) {//新建收货地址
            setTitle(R.string.add_address_add_title);
        } else {//编辑收货地址
            setTitle(R.string.add_address_edit_title);
        }

        setNavigation(R.drawable.common_ic_left_nav);

        //保存
        MenuTxt txt = new MenuTxt.MenuTxtBuilder(this)
            .setTitle(R.string.common_save)
            .setListener(new MenuItem.OnMenuItemClickListener() {
                @Override
                public boolean onMenuItemClick(MenuItem item) {
                    save();
                    return false;
                }
            }).build();

        addMenu(txt);
    }

    @Override
    protected void initLayout() {
        setContentView(R.layout.activity_add_address);
    }

    @Override
    protected void initViews() {
        ButterKnife.bind(this);

        if(mAddress != null) {
            mEtName.setText(mAddress.getName());
            mEtPhone.setText(mAddress.getPhone());
            mEtAddress.setText(mAddress.getAddress());
            mEtDetail.setText(mAddress.getDetail());
        }
    }

    /**
     * 保存地址
     */
    private void save() {
        //收货人姓名
        String name = mEtName.getText().toString().trim();

        if(UtilString.isBlank(name)) {
            UtilOuer.toast(this, R.string.add_address_name_empty);
            mEtName.requestFocus();
            return;
        }

        //手机号码
        String phone = mEtPhone.getText().toString().trim();

        if(UtilString.isBlank(phone)) {
            UtilOuer.toast(this, R.string.add_address_phone_empty);
            mEtPhone.requestFocus();
            return;
        }

        if(!UtilValidate.isPhone(phone)) {
            UtilOuer.toast(this, R.string.user_phone_error);
            mEtPhone.requestFocus();
            return;
        }

            //省市区
        String address = mEtAddress.getText().toString().trim();

        if(UtilString.isBlank(address)) {
            UtilOuer.toast(this, R.string.add_address_address_empty);
            mEtAddress.requestFocus();
            return;
        }

        //详细地址
        String detail = mEtDetail.getText().toString().trim();

        if(UtilString.isBlank(detail)) {
            UtilOuer.toast(this, R.string.add_address_detail_empty);
            mEtDetail.requestFocus();
            return;
        }

        mAddress.setName(name);
        mAddress.setPhone(phone);
        mAddress.setDetail(detail);
        mAddress.setAddress(address);

        // 发送意见反馈
        AgnettyFuture future = OuerApplication.mOuerFuture.editAddress(mAddress,
                new OuerFutureListener(this){

                    public void onStart(AgnettyResult result) {
                        setWaitingDialog(true);
                    }

                    @Override
                    public void onComplete(AgnettyResult result) {
                        super.onComplete(result);
                        setWaitingDialog(false);
                        UtilOuer.toast(EditAddressActivity.this, R.string.add_address_save_success);
                        finish();
                    }

                    public void onNetUnavaiable(AgnettyResult result) {
                        super.onNetUnavaiable(result);
                        setWaitingDialog(false);
                    }

                    @Override
                    public void onException(AgnettyResult result) {
                        super.onException(result);
                        setWaitingDialog(false);
                        UtilOuer.toast(EditAddressActivity.this, R.string.add_address_save_failure);
                    }
                });

        attachDestroyFutures(future);
    }
}
